package org.qqcui.designpattern.principles.dp;

import java.util.ArrayList;
import java.util.List;

/**
 * 迪米特法则示例一，违反迪米特法则的情况
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2020-10-14 21:57
 **/
public class Demter1 {
	public static void main(String[] args) {
		SchoolManager manager = new SchoolManager();
		manager.printAllEmployeeInfo(new CollageManager());
	}
}

/**
 * 学校总部员工类
 */
class Employee {
	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}

/**
 * 学院员工类
 */
class CollageEmployee {
	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}
}

/**
 * 学院员工管理类
 */
class CollageManager {

	public List<CollageEmployee> getAllEmployee() {
		List<CollageEmployee> collageEmployees = new ArrayList<CollageEmployee>();
		for (int i = 0; i < 10; i++) {
			CollageEmployee collageEmployee = new CollageEmployee();
			collageEmployee.setId(i);
			collageEmployees.add(collageEmployee);
		}
		return collageEmployees;
	}
}

/**
 * 学校总部员工管理类
 * 分析：
 * 1.SchoolManager类的直接朋友有Employee、CollageManager这两个类（直接朋友：成员变量，方法参数，返回值）
 * 2.SchoolManager类的陌生类CollageEmployee（在方法中的局部变量）
 * 3.改进方式：将陌生类的局部变量放到其对应的管理类中去实现即可
 */
class SchoolManager {

	public List<Employee> getAllEmployee() {
		List<Employee> employees = new ArrayList<Employee>();
		for (int i = 0; i < 5; i++) {
			Employee employee = new Employee();
			employee.setId(i);
			employees.add(employee);
		}
		return employees;
	}

	public void printAllEmployeeInfo(CollageManager cm) {
		System.out.println("================学院员工信息====================");
		List<CollageEmployee> list1 = cm.getAllEmployee();
		for (CollageEmployee employee : list1) {
			System.out.println("学院员工id: " + employee.getId());
		}

		System.out.println("=================学校总部员工信息======================");
		List<Employee> list2 = this.getAllEmployee();
		for (Employee employee : list2) {
			System.out.println("学校总部员工id: " + employee.getId());
		}
	}
}